package com.lihh.demo;

import java.util.concurrent.*;

public class AllOfTest001
{
    public static void main(String[] args) throws ExecutionException, InterruptedException
    {
        ExecutorService executorService = Executors.newCachedThreadPool();

        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程B");
            return "B";
        }, executorService);

        CompletableFuture<String> stringCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程C");
            return "C";
        }, executorService);

        CompletableFuture.allOf(CompletableFuture.runAsync(() -> {
            System.out.println("线程A");
        }, executorService), stringCompletableFuture, stringCompletableFuture1).thenAcceptAsync((res) -> {
            System.out.println("这是执行结果: " + res + ", " + Thread.currentThread().getName());
            System.out.println("执行最终结果");
        }, executorService);

        System.out.println(stringCompletableFuture.get() + " | " + Thread.currentThread().getName());
    }
}
